package edu.pitt.myptm.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;

import edu.pitt.myptm.DeleteCommand;
import edu.pitt.myptm.MyCommand;
import edu.pitt.myptm.datamanager.DataLoader;
import edu.pitt.myptm.datamanager.DataManager;
import edu.pitt.myptm.scheduler.LockContent;
import edu.pitt.myptm.scheduler.LockGranted;
import edu.pitt.myptm.scheduler.LockTuple;
import edu.pitt.myptm.scheduler.Scheduler;
import edu.pitt.myptm.transactionmanger.TransactionManager;


public class MyPTM {
	static TransactionManager tm = null;
	static DataManager dm = null;
	public void defaultLoad() throws FileNotFoundException {
		//DataLoader.loadFile("sampleDataFile.txt");
		tm = new TransactionManager();
//		tm.addFile("sampleReadTrx2.txt");
//		tm.addFile("sampleWriteTrx2.txt");
//		tm.addFile("sampleWriteTrx3.txt");
//		tm.addFile("sampleWriteTrx4.txt");
		
//		tm.addFile("sampleDeleteMatch1.txt");
//		tm.addFile("sampleDeleteMatch2.txt");
//		tm.addFile("sampleDeleteMatch3.txt");
		
//		tm.addFile("sampleProcessTrx2.txt");
//		tm.addFile("sampleProcessTrx3.txt");
//		tm.addFile("sampleProcessTrx4.txt");
//		DataLoader.loadFile("ds1/X.txt",8000);
		
		DataLoader.loadFile("X.txt",8311);
//		tm.addFile("ds1/aTrx.txt");
//		tm.addFile("ds1/shrtP.txt");
		

//		tm.addFile("ds2/aTrx.txt");
//		tm.addFile("ds2/rTrx.txt");
//		

//		tm.addFile("ds3/rTrx.txt");
//		tm.addFile("ds3/shrtP.txt");
//		

//		tm.addFile("ds4/rTrx.txt");
//		tm.addFile("ds4/wTrx.txt");
//		

		tm.addFile("ds5/aTrx.txt");
		tm.addFile("ds5/rTrx.txt");
		tm.addFile("ds5/shrtP.txt");
		tm.addFile("ds5/wTrx.txt");

		
//		tm.addFile("sampledeadlock1.txt");
//		tm.addFile("sampledeadlock2.txt");
//		tm.addFile("sampledeadlock3.txt");
		
//		tm.addFile("sampleSingle.txt");
		
		dm = new DataManager(1024);
		dm.setPageLimit(8000);
		tm.switchToRB();
//		tm.switchToRandom();
//		tm.setSeed(1000);
		tm.init();
	}
	
	public void initManager(String[] dataFiles,String[] scriptFiles,int bufferPageNum,int pageLimit,boolean isRandom,long seed) throws FileNotFoundException {
		for(int i = 0;i<dataFiles.length;i++) {
			DataLoader.loadFile(dataFiles[i],pageLimit);
		}
		tm = new TransactionManager();
		for(int i = 0;i<scriptFiles.length;i++) {
			tm.addFile(scriptFiles[i]);
		}
		dm = new DataManager(bufferPageNum);
		dm.setPageLimit(pageLimit);
		if(!isRandom) {
			tm.switchToRB();
		} else {
			tm.setSeed(seed);
			tm.switchToRandom();
		}
		tm.init();
	}
	
	public static void main(String[] args) {
		try {
		MyPTM myptm = new MyPTM();
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter 0 if want to use default settings, enter 1 to input parameters:");
		String option = sc.next();
		boolean isDefault = false;
		if(option.equals("0")) {
			isDefault = true;
			myptm.defaultLoad();
		} else {
			System.out.println("Input the data files, all files in one line, split by comma\n");
			System.out.println("Example: 1.txt,2.txt,3.txt");
			String datafilestr = sc.next();
			String[] dataFiles = datafilestr.split(",");
			System.out.println("Input the script files, all files in one line, split by comma\n");
			System.out.println("Example: s1.txt,s2.txt,s3.txt");
			String scriptfilestr = sc.next();
			String[] scriptFiles = scriptfilestr.split(",");
			System.out.println("Input the pages of the buffer, each page is 512 bytes");
			int bufferPageNum = sc.nextInt();
			System.out.println("Input the pageLimit of the data file, each page is 512 bytes");
			int pageLimit = sc.nextInt();
			System.out.println("Input the mode of transaction manager you want, 1 for round robin, 2 for random");
			int mode = sc.nextInt();
			boolean isRandom = false;
			if(mode == 1) {
				isRandom = false;
				myptm.initManager(dataFiles, scriptFiles, bufferPageNum, pageLimit, isRandom, -1);
			}
			else {
				isRandom = true;
				System.out.println("Input the seed, seed is a long integer");
				long seed = sc.nextLong();
				myptm.initManager(dataFiles, scriptFiles, bufferPageNum, pageLimit, isRandom, seed);
			}
			
			
		}
		
			Scheduler S= new Scheduler();
			Hashtable<LockTuple,LockContent> lock_table= new Hashtable<LockTuple,LockContent>();
			Hashtable<Integer, LockGranted> lock_holder_waitting_list= new Hashtable<Integer, LockGranted>();
			ArrayList<MyCommand> commit_list=new ArrayList<MyCommand>();	
			
			S.Schedule_Transactions(tm,dm);
			dm.exit();
		}catch(Exception e) {
			e.printStackTrace();
		}
			
/*			MyCommand cmd = tm.loadNext();
			while(cmd!=null) {
				dm.execute(cmd);
				cmd = tm.loadNext();				
			}*/
		} 
	
/*	public static void main(String[] args) {
		
		TransactionManager tm = new TransactionManager();
		tm.addFile("sampleReadTrx2.txt");
//		tm.addFile("sampleWriteTrx.txt");
		tm.addFile("sampleWriteTrx2.txt");
	//	tm.addFile("sampleWriteTrx3.txt");
		//tm.addFile("sampleWriteTrx4.txt");
		//File f = new File("sampleDataFile.txt");
		DataManager dm = new DataManager(1024);
		dm.setPageLimit(1024);
//		String toFile = "sample.txt";
//		dm.addFileOperator(toFile);
//		dm.getFileOperator(toFile).deleteFile();
//		dm.addFileOperator(toFile);
		
		try {
//			tm.switchToRandom();
			tm.switchToRB();
			tm.setSeed(1000);
			tm.init();
			Scheduler S= new Scheduler();
			Hashtable<LockTuple,LockContent> lock_table= new Hashtable<LockTuple,LockContent>();
			Hashtable<Integer, LockGranted> lock_holder_waitting_list= new Hashtable<Integer, LockGranted>();
			ArrayList<MyCommand> commit_list=new ArrayList<MyCommand>();
			
			
			S.Schedule_Transactions(tm,dm);
			
			MyCommand cmd = tm.loadNext();
			while(cmd!=null) {
				dm.execute(cmd);
				cmd = tm.loadNext();
				//dm.executeRead(cmd)
				
			}
			System.out.println("OUT");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}*/
}
